var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
        isfree:0,
        selectedDate:'',
        datalist: [],
        pagenum: 1,
        cateArr:[],
        pics:[],
		    cindex:0,
        isagree:true,
        id:0,
        selectIds:[],
        showleixing:false,
        region:[]
	},
	_onLoad:function(){
        var that = this;
        var id = 0;
        if(that.options.id){
            id = that.options.id;
            that.setData({id:id})
        }
		$.get('yuxun/fatiediaodian',{id:id},function(res){
			if(res.status == 2){
				$.alert(res.msg,function(){
					if(res.tourl){
						$.goto(res.tourl,'redirect');
					}
				});
				return ;
			}
			var clist = res.clist
			var cateArr = [];
			for(var i in clist){
				cateArr.push(clist[i].name)
            }
            
            res.pic = res.info ? [res.info.pic] : [];

			var pics = res.info ? res.info.pics : '';
			if(pics){
				pics = pics.split(',');
			}else{
				pics = [];
			}
			
			res.cateArr = cateArr;
			res.pics = pics;
	
			console.log(res)
			that.setData(res)
		});
    },
    showleixing:function(){
        this.setData({showleixing:true})
    },
    getUserProvince:function(e)
    {
       this.setData({
           region:e.detail.value,    //将用户选择的省市区赋值给region
           province:e.detail.value[0],
           city:e.detail.value[1],
           district:e.detail.value[2],
          //  'info.diqu':e.detail.value
       })
      //  this.getDataList()
       console.log('地区',this.data.region,this.info,e)
    },
    hideleixing:function(){
        this.setData({showleixing:false})
    },
    changeTagType2:function(e){
        var  id = e.currentTarget.dataset.id;
        var selectIds = this.data.selectIds;
       
        var idx = e.currentTarget.dataset.index;
        let clist = [...this.data.clist];

        let obj = {};
        clist.forEach((item, index) => {
            if (idx === index) {
                obj.id = item.id;
                obj.name = item.name;
                obj.select = !item.select;
                clist.splice(index, 1, obj);
                obj = {};
            }
        });

        var selectIds = [];
        for(var i=0; i<clist.length;i++){
            if(clist[i].select==true){
                selectIds.push(clist[i].id)
            }
        }
        console.log(selectIds)
        
        this.setData({clist:clist,selectIds:selectIds})
    },
    freeChange:function(e){
        var isfree = e.detail.value;
        console.log(isfree)
		this.setData({isfree:isfree})
	},
	cateChange:function(e){
		this.setData({cindex:e.detail.value})
    },
    handleDateChange: function (event) {
        const selectedDate = event.detail.value;
        console.log(selectedDate)
        this.setData({
            selectedDate: selectedDate,
        });
    },
	locationSelect:function(){
		var that = this
		wx.chooseLocation({success:function(res){
			console.log(res)
			that.setData({
				'info.address':res.name,
				'info.latitude':res.latitude,
				'info.longitude':res.longitude
			})
		}})
	},
	subform:function(e){
		var that = this;
    var info = e.detail.value;
    //var selectIds = this.data.selectIds;
    // if(info.diqu ==''){
    //   $.error('请选择地区');return false;
    // }
		if(info.name==''){
			$.error('请输入钓点名称');return false;
		}
		if(info.latitude==''){
			$.error('请选择坐标');return false;
		}
		if(info.address==''){
			$.error('请输入详细地址');return false;
        }
		if(info.pics==''){
			$.error('请上传钓点图片');return false;
    }
   
		//info.cid = that.data.clist[that.data.cindex].id
		// if(that.data.info && that.data.info.id){
		// 	info.id = that.data.info.id;
        // }
        var id = that.data.id;

        //info.isfree = that.data.isfree;
        
		$.post("yuxun/fatiediaodian",{info:info,diqu:that.data.region,id:id,isdiaochang:1},function(res){
      console.log('提交内容',info,id)
			$.error(res.msg);
			setTimeout(function(){
                $.goback(true)
			},1000)
		},'提交中');
    },
    isagreeChange:function(e){
		console.log(e.detail.value)
		var val = e.detail.value
		if(val.length > 0){
			this.setData({isagree:true})
		}else{
			this.setData({isagree:false})
		}
	},
	showxieyi:function(){
		this.setData({showxieyi:true})
	},
	hidexieyi:function(){
		this.setData({showxieyi:false,isagree:true})
	},
	setname:function(e){
		this.setData({'info.name':e.detail.value})	
    },
    setprice:function(e){
		this.setData({'info.price':e.detail.value})	
    },
    setshuishen:function(e){
		this.setData({'info.shuishen':e.detail.value})	
    },
    setganchang:function(e){
		this.setData({'info.ganchang':e.detail.value})	
    },
    setyonger:function(e){
		this.setData({'info.yonger':e.detail.value})	
    },
    setfield(e){
        var field = e.currentTarget.dataset.name;
        if(field=="diaowei"){
            this.setData({'info.diaowei':e.detail.value})
        }
        if(field=="diyu"){
            this.setData({'info.diyu':e.detail.value})
        }
        if(field=="fangyu"){
            this.setData({'info.fangyu':e.detail.value})
        }
        if(field=="huiyu"){
            this.setData({'info.huiyu':e.detail.value})
        }
        
    },
}))